//
//  NSObject+runtime.m
//  WeiZi
//
//  Created by Joshpell on 2018/3/16.
//  Copyright © 2018年 Meniny. All rights reserved.
//

#import "NSObject+runtime.h"
#import <objc/message.h>


@implementation NSObject (runtime)

+(id)objectWithClassName:(NSString *)className propertyDict:(NSDictionary *)params{
    
    NSString *vcName = className;
    
    Class tempClass = NSClassFromString(vcName);
    id objc = [[tempClass alloc] init];
    for (NSString *key in params.allKeys) {
        
        id value = params[key];
        
        /*判断当前属性是不是所需类属性*/
        objc_property_t property = class_getProperty(tempClass, key.UTF8String);
        unsigned int outCount = 0;
        objc_property_attribute_t *attributeList = property_copyAttributeList(property, &outCount);
        
        //生成setter方法，并用objc_msgSend调用
        NSString *methodName = [NSString stringWithFormat:@"set%@%@:",[key substringToIndex:1].uppercaseString,[key substringFromIndex:1]];
        SEL setter = sel_registerName(methodName.UTF8String);
        if ([objc respondsToSelector:setter]) {
            
            ((void (*) (id,SEL,id)) objc_msgSend) (objc,setter,value);
        }
        free(attributeList);
    }
    
    return objc;

}

@end
